package com.shallowUniverse.process.utils;


import com.shallowUniverse.process.context.user.SysUser;
import com.shallowUniverse.process.entity.Approval;
import com.shallowUniverse.process.entity.ProcessDefinition;
import com.shallowUniverse.process.entity.ProcessNodeDefinition;
import com.shallowUniverse.process.exception.*;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.List;

/**
 * @author gs
 * @date create in 2020/12/11 17:10
 */
public class AssertUtil {

    /**
     * description: 流程是否为空 <br/>
     * author: Shuai Guo   <br/>
     * date: 2021/7/20 <br/>
     * params: [processDefinition] <br/>
     * return: void <br/>
     * version: 1.0
     */
    public static void processDefinitionNotFound(ProcessDefinition processDefinition) throws ProcessNotFoundException {
        if (processDefinition == null) {
            throw new ProcessNotFoundException("流程未找到");
        }
    }

    /**
     * description: 主键是否为空 <br/>
     * author: Shuai Guo   <br/>
     * date: 2021/7/20 <br/>
     * params: [object] <br/>
     * return: void <br/>
     * version: 1.0
     */
    public static void primaryKeyNotNull(Object object) throws PrimaryKeyNotFoundException {
        if (object == null) {
            throw new PrimaryKeyNotFoundException("目标主键未找到");
        }
    }

    /**
     * description: 审批单不存在了 <br/>
     * author: Shuai Guo   <br/>
     * date: 2021/7/20 <br/>
     * params: [approval] <br/>
     * return: void <br/>
     * version: 1.0
     */
    public static void approvalNotNull(Approval approval) throws ApprovalNotExistException {
        if (approval == null) {
            throw new ApprovalNotExistException("审批单不存在");
        }
    }

    /**
     * description: 用户未登陆或权限不足 <br/>
     * author: Shuai Guo   <br/>
     * date: 2021/7/20 <br/>
     * params: [value] <br/>
     * return: void <br/>
     * version: 1.0
     */
    public static void notAuthentication(Object value) throws AuthenticationException {
        if(value instanceof SysUser){
            if (ObjectUtils.isEmpty(value)) {
                throw new AuthenticationException("用户未登录，请设置当前用户到processUserHolder中");
            }
        } else if(value instanceof Collection) {
            if (CollectionUtils.isEmpty((Collection<?>) value)) {
                throw new AuthenticationException("权限不足，请检查您的权限");
            }
        }
    }

    /**
     * description: 流程节点定义有误 <br/>
     * author: Shuai Guo   <br/>
     * date: 2021/7/20 <br/>
     * params: [processNodeDefinitionsn] <br/>
     * return: void <br/>
     * version: 1.0
     */
    public static void isProcessNodeDefinitionError(List<ProcessNodeDefinition> processNodeDefinitions) throws ProcessNodeDefinitionErrorException {
        if ((CollectionUtils.isEmpty(processNodeDefinitions)) || CollectionUtils.isEmpty(processNodeDefinitions)) {
            throw new ProcessNodeDefinitionErrorException("流程节点定义有误");
        }
    }

    /**
     * description: 流程未完成异常 <br/>
     * author: Shuai Guo   <br/>
     * date: 2021/7/15 <br/>
     * params: [approvals] <br/>
     * return: void <br/>
     * version: 1.0
     */
    public static void processNotCompleted(List<Approval> approvals) throws ProcessNotCompletedException {
        if (!CollectionUtils.isEmpty(approvals)) {
            throw new ProcessNotCompletedException("存在未完成的流程");
        }
    }
}
